/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.capex.javaee.cookies;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Acer
 */
public class muestraCookiesHeader extends HttpServlet {

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {
            String username = request.getParameter("usuario");
            String password = request.getParameter("password");

            Cookie cookieUser = new Cookie(username, password);

            response.addCookie(cookieUser);

            String url = request.getHeader("referer");
            response.sendRedirect(url);

        } finally {
            out.close();
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try {

            Enumeration<String> headerNames = request.getHeaderNames();
            Cookie[] cookies = request.getCookies();

            out.println("<html>");
            out.println("<head>");
            out.println("<title>Showing Cookies and Headers</title>");
            out.println("</head>");
            out.println("<body>");

            out.println("<h4>Header Recieved</h4>");
            while (headerNames.hasMoreElements()) {
                String headerName = headerNames.nextElement();
                Enumeration<String> headerValues = request.getHeaders(headerName);
                out.println("<p><b>" + headerName + "</b>");
                while (headerValues.hasMoreElements()) {
                    String temp = headerValues.nextElement();
                    out.println(temp + "</p>");
                }
            }

            out.println("<p>-----------------------------------------</p>");
            out.println("<h4>Cookies Stored</h4>");
            for (Cookie temp : cookies) {
                out.println("<p><b>Cookie Name: </b>" + temp.getName() + "</p>");
            }

            out.println("</body>");
            out.println("</html>");

        } finally {
            out.close();
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
